/**
 * date:2021/02/27
 * author:LeapMary
 * version:2.0
 * description:js常用扩展
 */
layui.define(["element", "layer", "jquery"], function (exports) {

    var element = layui.element,
        layer = layui.layer,
        $ = layui.$;

    var chinAdmin = {
        /**
         * index页面通用js:todo::需要更精简化
         */
        index: function (layui) {

            var $ = layui.jquery, form = layui.form, table = layui.table, laydate = layui.laydate,
                chinAdmin = layui.chinAdmin;

            if(layui.$Modules == undefined || layui.$Modules == ''){
                layui.$Modules = 'chinadmin';
            }

            if (layui.$cols != undefined && layui.$cols != '') {
                //初始化表格,为每一个赋值。
                table.render({
                    elem: '#currentTableId',
                    url: "/"+layui.$Modules+"/admin/" + layui.$class + "/index?" + encodeURI(layui.$ThisGet),
                    toolbar: '#toolbar',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '列表',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [layui.$cols],                //列表项
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 15,
                    page: true,
                    skin: 'line',
                    done: function (e) {
                        if (e.code == 400) {
                            layer.msg(e.msg);
                        }
                    }
                });
            }

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {

                var result = data.field;

                //带上搜索类型
                for (var i in result) {
                    var this_data = result[i];
                    var search_type = $("input[name='" + i + "']").attr('data-search-type');

                    if (search_type == undefined) {
                        search_type = '=';
                    }

                    result[i] = {};

                    switch (search_type) {
                        case '>=':
                            result[i]['begin'] = {};
                            result[i]['begin'].val = this_data;

                            result[i]['end'] = {};
                            result[i]['end'].val = $("#" + i + "_end").val();

                            if (result[i]['end'].val < result[i]['begin'].val) {
                                layer.msg("开始不能大于结束");
                                return false;
                            }
                            break;
                        default:
                            result[i].val = this_data;
                            result[i].search = search_type;
                    }

                }

                var result = JSON.stringify(result);

                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        searchParams: result   //搜索参数
                    }
                }, 'data');

                return false;

            });

            //toolbar监听表格头部添加删除
            table.on('toolbar(currentTableFilter)', function (obj) {

                if (obj.event === 'add') {  // 监听添加操作

                    var index = layer.open({
                        title: '添加',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: "/"+layui.$Modules+"/admin/" + layui.$class + "/create",
                    });

                } else if (obj.event === 'delete') {  // 监听删除操作

                    var checkStatus = table.checkStatus('currentTableId')
                        , datac = checkStatus.data;

                    if(datac.length == 0){
                        layer.msg("必须选中数据");
                        return false;
                    }

                    $.ajax({
                        type: "POST",
                        url: "/"+layui.$Modules+"/admin/" + layui.$class + "/delete",
                        timeout: 100000, //超时时间设置，单位毫秒 为一秒
                        data: {
                            'datac': datac
                        },
                        dataType: "json",      //如果有此选项成功返回的json会自动变成对象，如果没有需要用JSON.parse()转换
                        success: function (data) {
                            layer.msg(data.msg);
                            table.reload('currentTableId');
                        }
                    });

                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
            });

            //编辑删除操作
            table.on('tool(currentTableFilter)', function (obj) {

                var data = obj.data;

                typeof self_extend === "function" ? self_extend(obj, layui) : false;

                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: "/"+layui.$Modules+"/admin/" + layui.$class + "/create?id=" + data[layui.$this_id],
                    });

                    return false;

                } else if (obj.event === 'delete') {

                    layer.confirm('真的删除行么', function (index) {

                        $.ajax({
                            type: "POST",
                            url: "/"+layui.$Modules+"/admin/" + layui.$class + "/del",
                            timeout: 100000, //超时时间设置，单位毫秒 为一秒
                            data: {
                                'id': data[layui.$this_id]
                            },
                            dataType: "json",      //如果有此选项成功返回的json会自动变成对象，如果没有需要用JSON.parse()转换
                            success: function (data) {
                                layer.msg(data.msg);
                            }
                        });

                        //界面效果
                        obj.del();
                        layer.close(index);

                    });

                }
            });

            //头部点击事件
            $("body").delegate("th", "dblclick", function () {

                var data_field = $(this).attr("data-field");
                var td_width = $(this).width();

                layer.prompt({title: '修改宽度:' + td_width, formType: 3}, function (num, index) {

                    //修改宽度
                    $.ajax({
                        type: "POST",
                        url: "/"+layui.$Modules+"/admin/forms/update",
                        timeout: 100000, //超时时间设置，单位毫秒 为一秒
                        data: {
                            td_width: num,
                            data_field: data_field,
                            table: layui.$table
                        },
                        dataType: "json",      //如果有此选项成功返回的json会自动变成对象，如果没有需要用JSON.parse()转换
                        success: function (data) {

                            layer.msg(data.msg);
                            window.location.reload();

                        },
                    });

                    layer.close(index);

                });

            });

            //图片放大展示
            $("body").delegate(".show_this", "click", function () {
                //页面层
                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['100%', '100%'], //宽高
                    content: '<img src="' + $(this).attr('src') + '">'
                });

            });

        },

        /**
         * create页面通用js:todo::需要更精简化
         */
        create: function (layui) {

            var $ = layui.jquery, form = layui.form, table = layui.table, laydate = layui.laydate,
                chinAdmin = layui.chinAdmin;

            $("body").delegate(".choice_img", "click", function () {

                var field = $(this).attr("data-field");
                var field_val = $("." + field).val();
                var this_src = $(this).attr("src");

                var field_arr = new Array(); //定义一数组
                field_arr = field_val.split(","); //字符分割

                var that = this;

                //询问框
                layer.confirm('确定要删除本图片？', {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    var img_src = $(this).attr("src");

                    var index = field_arr.indexOf(this_src);
                    if (index >= 0) {
                        field_arr.splice(index, 1);
                    }
                    $("." + field).val(field_arr.toString());

                    $(that).remove();

                    layer.msg("删除成功");

                }, function () {

                });

                return false;

            });

            $(".layui-btn-primary").click(function () {
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });

            //标准表单提交进行相关处理
            form.on('submit(Submit_immediately)', function (data) {
                layui.formSubmit(data);
                return false;
            });

        }
    };

    exports("chinAdmin", chinAdmin);
});
